package com.grupoconstruobras.sigo.exhumaciones.diligencia;

import java.text.DecimalFormat;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

@FacesConverter("margenGPSConverter")
public class MargenGPSConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		DecimalFormat df = new DecimalFormat("#00.0#");
		Double val = null;
		try {
			val = Double.parseDouble(value);
			df.setMaximumIntegerDigits(3);
			if (df.parse(df.format(val)).doubleValue() != val) {
				throw new ConverterException();
			}
		} catch (Exception e) {
			throw new ConverterException();
		}
		return val;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		DecimalFormat df = new DecimalFormat("###.##");
		Double val = (Double) value;
		return df.format(val).replaceAll(",", ".");
	}

}
